<template>
  <div>
    <!--  面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!--  卡片区域  -->
    <el-card>
      <el-row>
        <el-col :span="8">
          <!--  输入查询框 -->
          <el-input clearable @clear="getGoodsList" placeholder="请输入内容" v-model="queryInfo.query" class="input-with-select">
            <el-button @click="getGoodsList" slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <el-col class="add_goods" :span="10">
          <!--  添加商品按钮  -->
          <el-button type="primary" @click="addGoods">添加商品</el-button>
        </el-col>
      </el-row>
      <!--  商品数据列表  -->
      <el-table :data="goodsList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column width="650" prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格（元）"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量"></el-table-column>
        <el-table-column width="170" prop="add_time" label="创建时间">
          <template slot-scope="scope">
            {{scope.row.add_time | timeFilter}}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" icon="el-icon-edit"></el-button>
            <el-button type="warning" @click="deleteGoods(scope.row.goods_id)" size="mini" icon="el-icon-delete"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--  商品数据列表分页器 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryInfo.pagenum"
                     :page-sizes="[5, 10, 20,40]" :page-size="queryInfo.pagesize"
                     layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
  export default {
    name: "Goods",
    data() {
      return {
        /*  查询条件  */
        queryInfo: {
          query:'',
          pagesize: 5,
          pagenum: 3
        },
        goodsList: [],
        total: 0,
      }
    },

    created() {
      this.getGoodsList();
    },

    methods: {
      /*  添加商品  */
      addGoods(){
        this.$router.push('/goods/add');
      },
      /*  删除商品  */
      deleteGoods(rowId){
        console.log(rowId);
        this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          /*  确定删除该属性delete */
          const {data: deleteRes} = await this.$http.delete(`goods/${rowId}`);
          if (deleteRes.meta.status === 200) {
            this.$message.success("成功删除该商品");
            this.getGoodsList();
          } else {
            this.$message.error("删除商品失败");
          }
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      /*  获取商品列表数据 */
      async getGoodsList() {
        const {data: res} = await this.$http.get('goods', {params: this.queryInfo});
        console.log(res);
        if (res.meta.status === 200) {
          this.goodsList = res.data.goods;
          this.total = res.data.total;
        } else {
          this.$message.error('商品列表数据获取失败')
        }
      },
      /*  监听当前页的改变  */
      handleCurrentChange(nowCurrent) {
        this.queryInfo.pagenum = nowCurrent;
        this.getGoodsList()
      },
      /*  监听每页多少条的改变  */
      handleSizeChange(nowPageSize) {
        this.queryInfo.pagesize = nowPageSize;
        this.getGoodsList()
      },
    }
  }
</script>

<style scoped>
  .add_goods {
    margin-left: 15px;
  }
</style>
